
let text = require('../../../../common/util/text.js')
Page({
    data: {
        xPageReady : true ,
        xActicle : '' ,
        xId: '' ,
        xLikeState : false ,
        ids:'',
        scrollTop: -1
    },

    onLoad: function (options) {
        this.setData({
          ids:options.id
        })
        this.xInit(options.id)
    },
    onShareAppMessage: function () {
      return {
        title: this.data.xActicle.title
      };
    },
    onReachBottom:function(){
        wx.removeStorageSync(this.data.ids)
    },
    onPageScroll: function (e) {
				let that = this
				wx.setStorageSync(that.data.ids, e.scrollTop)
    },
    bindscrolltolower:function(){
				wx.removeStorageSync(this.data.ids)
    },
    scroll:function(e){
      wx.setStorageSync(this.data.ids, e.detail.scrollTop)
    },
    onShow:function(){
      let that =this;
    },
    xInit: async function (id) {
        let that = this
        await wx.cloud.callFunction({
            name: 'article',
            data: {
                action: "get_article_info",
                _id: id
            }
        }).then(res => {
            res.result.content=text.text(res.result.content)
              that.setData({
                  xActicle : res.result ,
                  xId: id ,
                  xLikeState : res.result.is_like
              })
              try {
                var value = wx.getStorageSync(that.data.ids)
                if(value){
                  that.setData({
                    scrollTop:-1
                  })
                  setTimeout( function () {
                    that.setData({
                      scrollTop:value
                    })
                  } , 100 )
                }
              } catch (e) {}
        })
        that.setData({
            xPageReady : false
        })

    },

    xLike: async function () {
        let that = this
        let id = that.data.xId

        wx.showLoading({
            title: '请稍等',
        })

        await wx.cloud.callFunction({
            name: 'article',
            data: {
                action: "add_article_like",
                dataObj: {
                    tag_id : id ,
                    source : 1
                }
            }
        }).then(res => {
            that.xInit(id)
        })

        wx.hideLoading()
    }
})